home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Dev / Meshwriter / pov3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  5.1 KB  |  160 lines

  1. /*
  2. **      $VER: pov3.c 1.00 (27.03.1999)
  3. **
  4. **      Creation date : 29.11.1998
  5. **
  6. **      Description       :
  7. **         Standart saver module for meshwriter.library.
  8. **         Saves the mesh as POV3 ascii file.
  9. **         POV z = Mesh y and POV y = Mesh z
  10. **
  11. **
  12. **      Written by Stephan Bielmann
  13. **
  14. */
  15.  
  16. /*************************** Includes *******************************/
  17.  
  18. /*
  19. ** Amiga includes
  20. */
  21. #include <dos/dos.h>
  22. #include <dos/stdio.h>
  23.  
  24. #include <clib/dos_protos.h>
  25. #include <clib/alib_stdio_protos.h>
  26.  
  27. /*
  28. ** Project includes
  29. */
  30. #include "meshwriter_private.h"
  31.  
  32. /********************** Private functions ***************************/
  33.  
  34. /********************** Public functions ****************************/
  35.  
  36. /********************************************************************\
  37. *                                                                    *
  38. * Name         : write3POV3                                          *
  39. *                                                                    *
  40. * Description  : Writes a standart POV3 ascii file.                  *
  41. *                                                                    *
  42. * Arguments    : povfile IN : An already opened file stream.         *
  43. *                mesh    IN : Pointer to the mesh.                   *
  44. *                                                                    *
  45. * Return Value : RCNOERROR                                           *
  46. *                RCWRITEDATA                                         *
  47. *                                                                    *
  48. * Comment      : No default material !                               *
  49. *                                                                    *
  50. \********************************************************************/
  51. ULONG write3POV3(BPTR povfile, TOCLMesh *mesh) {
  52.     UBYTE                         buffer[200];
  53.     TOCLMaterialNode            *mat=NULL;
  54.     TOCLPolygonNode            *pln=NULL;    
  55.     TOCLPolygonsVerticesNode    *plvi=NULL,*plv1=NULL,*plv2=NULL,*plv3=NULL;
  56.     TOCLVertex                    ver1,ver2,ver3;
  57.     TOCLFloat                    r,g,b;
  58.  
  59.  
  60.     /*
  61.     ** Write the header, default separator and info node
  62.     */
  63.     if (FPrintf(povfile,"/*\n** POV3-Scenefile.\n** Object name : %s\n",mesh->name)==ENDSTREAMCH) return(RCWRITEDATA);
  64.     if (mesh->copyright) {
  65.         if (FPrintf(povfile,"**\n** %s\n",mesh->copyright)==ENDSTREAMCH) return(RCWRITEDATA);
  66.     }
  67.     
  68.     if (FPuts(povfile,"*/\n\n#version 3.0\n\n")!=DOSFALSE) return(RCWRITEDATA);
  69.  
  70.     /*
  71.     ** Write the materials
  72.     */
  73.     if(mesh->materials.firstNode!=NULL) {
  74.         /* Declare each material as texture */
  75.         mat=mesh->materials.firstNode;
  76.         do {      
  77.             TOCLColor col=mat->diffuseColor;
  78.             TOCLFloat r=col.r,g=col.g,b=col.b;
  79.             
  80.             if (FPrintf(povfile,"#declare %s = texture {",mat->name)==ENDSTREAMCH) return(RCWRITEDATA);
  81.  
  82.             sprintf(buffer,"\n pigment {\n  color rgb <%g, %g, %g>\n  filter %g\n }\n",r/255,g/255,b/255,mat->transparency);
  83.             if (FPuts(povfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  84.  
  85.             sprintf(buffer," finish {\n  brilliance %g\n",mat->shininess);
  86.             if (FPuts(povfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  87.  
  88.             if(mat->transparency>0.0) {
  89.                 if (FPuts(povfile,"  refraction 1\n }\n}\n")!=DOSFALSE) return(RCWRITEDATA);
  90.             }
  91.             else {
  92.               if (FPuts(povfile,"  refraction 0\n }\n}\n")!=DOSFALSE) return(RCWRITEDATA);
  93.             }
  94.             
  95.             mat=mat->next;
  96.         } while(mat!=NULL);
  97.     }
  98.  
  99.     /*
  100.     ** Write the camera
  101.     */
  102.     sprintf(buffer,"\ncamera {\n location  <%g, %g, %g>\n look_at <%g, %g, %g>\n}\n",
  103.             mesh->camera.position.x,mesh->camera.position.y,mesh->camera.position.z,
  104.             mesh->camera.lookat.x,mesh->camera.lookat.y,mesh->camera.lookat.z);
  105.     if (FPuts(povfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  106.  
  107.     /*
  108.     ** Write the lightsource
  109.     */
  110.     r=mesh->light.color.r,g=mesh->light.color.g,b=mesh->light.color.b;    
  111.     r/=255,g/=255,b/=255;    
  112.     sprintf(buffer,"\nlight_source {\n <%g, %g, %g>\n color rgb <%g,%g,%g>\n}\n",
  113.             mesh->light.position.x,mesh->light.position.y,mesh->light.position.z,
  114.             r,g,b);
  115.     if (FPuts(povfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  116.     
  117.     /*
  118.     ** Write the mesh
  119.     ** The polygons as triangles, must be convex polygons !
  120.     */              
  121.     pln=mesh->polygons.firstNode;
  122.     if (pln!=NULL) {
  123.         if (FPuts(povfile,"\nmesh {\n")!=DOSFALSE) return(RCWRITEDATA);
  124.     
  125.         do {
  126.             /* Get the first point of the polygon, used to create all triangles with it */
  127.             if(pln->numberOfVertices>=3) {
  128.                 plv1=pln->firstNode;
  129.                 ver1=plv1->vertexNode->vertex;
  130.             
  131.                 plvi=plv1;
  132.                 do {    
  133.                     plv2=plvi->next;
  134.                     plv3=plv2->next;
  135.                 
  136.                     ver2=plv2->vertexNode->vertex;
  137.                     ver3=plv3->vertexNode->vertex;
  138.                 
  139.                     sprintf(buffer," triangle {\n  <%g, %g, %g>, <%g, %g, %g>, <%g, %g, %g>\n",ver1.x,ver1.z,ver1.y,
  140.                     ver2.x,ver2.z,ver2.y,ver3.x,ver3.z,ver3.y);
  141.                     if(FPuts(povfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  142.                 
  143.                     if(pln->materialNode!=NULL) {
  144.                         if (FPrintf(povfile,"  texture {\n   %s\n  }\n",pln->materialNode->name)==ENDSTREAMCH) return(RCWRITEDATA);
  145.                     }
  146.                 
  147.                     if (FPuts(povfile," }\n")!=DOSFALSE) return(RCWRITEDATA);
  148.                     
  149.                     plvi=plvi->next;
  150.                 } while(plv3->next!=NULL);
  151.             }
  152.             pln=pln->next;
  153.         } while(pln!=NULL);
  154.         if (FPuts(povfile,"}\n")!=DOSFALSE) return(RCWRITEDATA);
  155.     }
  156.  
  157.     return(RCNOERROR);
  158. }
  159.  
  160. /************************* End of file ******************************/